﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

using EF.Model.Base;
using EF.Model;
using EF.DAL.Base;
using EF.Util;

namespace EF.BLL.Base
{
    public class UserService
    {
        private UserDAO userDAL = new UserDAO();

        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="password">密码</param>
        /// <param name="userId">用户Id</param>
        /// <param name="roleId">角色Id</param>
        /// <returns>1,登录成功; 0,登录失败</returns>
        public int Login(string userName, string password, out int userId, out string role, out string realName)
        {
            return userDAL.Login(userName, password, out userId, out role, out realName);
        }

        /// <summary>
        /// 获取用户列表
        /// </summary>
        /// <param name="spl">查询条件</param>
        /// <param name="lang">语言</param>
        /// <param name="columns">显示的列</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">一页的记录数</param>
        /// <param name="orderBy">排序</param>
        /// <param name="rowsCount">总记录数</param>
        /// <returns></returns>
        public DataTable GetList(List<SqlParameterInfo> spl, string lang, string columns, int? pageIndex, int? pageSize, string orderBy, out int rowsCount)
        {
            return userDAL.GetList(spl, lang, columns, pageIndex, pageSize, orderBy, out rowsCount);
        }

        /// <summary>
        /// 获取历史用户
        /// </summary>
        /// <param name="lang">语言</param>
        /// <param name="columns">显示的列</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">一页的记录数</param>
        /// <param name="orderBy">排序</param>
        /// <param name="rowsCount">总记录数</param>
        /// <returns></returns>
        public DataTable GetHistory(string lang, string columns, int? pageIndex, int? pageSize, string orderBy, out int rowsCount)
        {
            return userDAL.GetHistory(lang, columns, pageIndex, pageSize, orderBy, out rowsCount);
        }

        /// <summary>
        /// 恢复被删除的用户
        /// </summary>
        /// <param name="userId">用户Id</param>
        /// <param name="updateUser">更新用户</param>
        /// <param name="lang">语言</param>
        /// <param name="msg">消息</param>
        /// <returns></returns>
        public int Restore(string userIds, int updateUser, string lang, out string msg)
        {
            return userDAL.Restore(userIds, updateUser, lang, out msg);
        }

        /// <summary>
        /// 获取用户Model
        /// </summary>
        /// <param name="lang">语言</param>
        /// <param name="userId">用户Id</param>
        /// <returns></returns>
        public UserModel GetModel(string lang, int userId)
        {
            return userDAL.GetModel(lang, userId);
        }

        /// <summary>
        /// 用户新建
        /// </summary>
        /// <param name="model">用户Model</param>
        /// <returns>1,新建成功;2,新建失败,用户名已存在;0,新建失败,数据库异常.</returns>
        public int Add(UserModel model, string lang, out string msg)
        {
            return userDAL.Add(model, lang , out msg);
        }

        /// <summary>
        /// 用户编辑
        /// </summary>
        /// <param name="model">用户Model</param>
        /// <returns>1,编辑成功;0,编辑失败,数据库异常.</returns>
        public int Edit(UserModel model, string lang , out string msg)
        {
            return userDAL.Edit(model, lang, out msg);
        }

        /// <summary>
        /// 用户逻辑删除
        /// </summary>
        /// <param name="userId">被逻辑删除用户Id</param>
        /// <param name="updateUser">执行逻辑删除用户Id</param>
        /// <returns>1,逻辑删除成功;0,逻辑删除失败,数据库异常.</returns>
        public int LogicDel(string userIds, int updateUser, string lang, out string msg)
        {
            return userDAL.LogicDel(userIds, updateUser, lang, out msg);
        }

        /// <summary>
        /// 导出
        /// </summary>
        /// <param name="columns">导出列索引</param>
        /// <param name="orderBy">排序</param>
        /// <param name="columnNames">列名</param>
        /// <param name="lang">语言</param>
        public void Export(List<SqlParameterInfo> spl, string columns, string orderBy, string columnNames, string lang)
        {
            int rowsCount;
            DataTable dt = this.GetList(spl, lang, columns, null, null, orderBy, out rowsCount);

            ExcelHelp.ExportToExcel(dt, "user_export", columnNames);
        }
    }
}